package net.hzlj.platform.enterprise.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.bugly.beta.Beta;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.activity.AboutActivity;
import net.hzlj.platform.enterprise.activity.HomeActivity;
import net.hzlj.platform.enterprise.activity.LoginActivity;
import net.hzlj.platform.enterprise.activity.PersonalInformationActivity;
import net.hzlj.platform.enterprise.beans.UserBean;
import net.hzlj.platform.enterprise.beans.ui.Category;
import net.hzlj.platform.enterprise.common.EventBusParameter;
import net.hzlj.platform.enterprise.helper.ImageLoaderHelper;
import net.hzlj.platform.enterprise.utils.StringUtils;
import net.hzlj.rl.zhangjiajie.R;
import net.hzlj.platform.enterprise.task.Task;
import net.hzlj.platform.enterprise.utils.AppPreferencesUtils;
import net.hzlj.platform.enterprise.utils.PreferencesUtils;
import android.util.Log;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * /**
 *
 * @Description: 我
 * @Author: Jason Zhang
 * @Date: 2016-01-08 16:30:23
 */
public class ProfileFragment extends BaseFragment {


    private static final String TAG = ProfileFragment.class.getSimpleName();
    @BindView(R.id.header_ImageView)
    ImageView mHeaderImageView;
    @BindView(R.id.name_textView)
    TextView mNameTextView;
    @BindView(R.id.account_textView)
    TextView mAccountTextView;
    @BindView(R.id.about_button)
    LinearLayout mAboutButton;
    @BindView(R.id.update_button)
    LinearLayout mUpdateButton;
    @BindView(R.id.exit_button)
    LinearLayout mExitButton;
    @BindView(R.id.profile_button)
    LinearLayout mProfileButton;

    private Category mParentCategory;
    private int mPosition;

    private HomeActivity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "--------> onCreate");
        mParentCategory = (Category) tag;
        if (arg1 != null) {
            mPosition = (Integer) arg1;
        }
        mActivity = (HomeActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(TAG, "--------> onCreateView");
        View view = inflater.inflate(R.layout.fragment_profile, null, true);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData(int netState) {
        super.initData(netState);
        Log.i(TAG, "--------> initData netState:" + netState);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "--------> onResume");
        setNetInitState(NetState.INIT_SUCCESS);
        updateData();
    }

    private void updateData(){
        UserBean user = AppPreferencesUtils.getObject(BootApp.getInstance(),PreferencesUtils.KEY_USER_INFO,UserBean.class);
        if (user != null) {
            mNameTextView.setText(user.getName());
            mAccountTextView.setText(user.getIdentityNO());
            if (StringUtils.isNotEmpty(user.getUploadUrl()) && user.getUploadUrl().contains("http://")){
                Glide.with(this).load(user.getUploadUrl())
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mHeaderImageView);
            }

        }
    }

    @Override
    public void onFrameResume() {
        super.onFrameResume();
        Log.i(TAG, "--------> onFrameResume");
        updateData();
    }


    @Override
    protected void onEventRunBackground(EventBusParameter parameter) {
        super.onEventRunBackground(parameter);
        if (EventBusParameter.ACTION_UPDATE_USER.equals(parameter.action)){
            updateData();
        }
    }

    @Override
    public void onDataCallback(Task task) {
        super.onDataCallback(task);
    }

    @OnClick({R.id.about_button, R.id.update_button, R.id.exit_button,R.id.profile_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.about_button:
                startActivity(new Intent(mActivity, AboutActivity.class));
                break;
            case R.id.update_button:
                Beta.checkUpgrade();
//                startActivity(new Intent(mActivity, LiveVideoActivity.class));
                break;
            case R.id.exit_button:
//                mActivity.unbindPush();
//                BootApp.getInstance().exitLogin();
                Intent intent = new Intent(mActivity, LoginActivity.class);
                intent.putExtra(LoginActivity.KEY_LOGIN_FROM,true);
                startActivity(intent);
//                mActivity.finish();
                break;
            case R.id.profile_button:
                //进入人员详情页面
                startActivity(new Intent(mActivity, PersonalInformationActivity.class));
                break;
        }
    }

}
